package com.aaa.service.impl;

import com.aaa.entity.UserLogin;
import com.aaa.mapper.UserLoginMapper;
import com.aaa.result.Result;
import com.aaa.service.UserLoginService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Set;


@Service
public class UserLoginServiceImpl implements UserLoginService {

    @Autowired
    UserLoginMapper mapper;


    //登陆
    @Override
    public UserLogin Login(String username) {
        //1,根据用户名查询
        QueryWrapper<UserLogin> userLoginQueryWrapper = new QueryWrapper<UserLogin>();
        userLoginQueryWrapper.eq("username",username);
        UserLogin userLogin = mapper.selectOne(userLoginQueryWrapper);
        //2,判断查询出来的对象是否为null
        if (userLogin==null){
            //抛出 没有找到账号异常
            throw   new UnknownAccountException();
        }
        //3,判断账号的状态
        if (userLogin.getState().equals(UserLogin.STATUS_LOCKED)){
            //账号锁定异常
            throw  new LockedAccountException();
        }
        //4,整理数据 返回给认证
        return userLogin;
    }


    //根据登陆名查询权限
    @Override
    public Result queryPermissionsByLoginUserName(String username) {
        Set<String> strings = mapper.queryPermissionsByLoginUserName(username);
        if (strings.size()<0 ||strings==null){
            return Result.error("对不起该用户没有权限");
        }
        return Result.success(strings);
    }

}
